package identity;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.activiti.engine.impl.pvm.delegate.ActivityExecution;
import org.activiti.engine.runtime.Execution;
import org.springframework.stereotype.Service;

import datasource.DataSource;

@Service("userService")
public class UserServiceImpl implements IUserService {

	@Override
	public List<String> findCandidateUsers(Execution execution) {
		ActivityExecution activityExecution = (ActivityExecution) execution;

		Set<Role> roles = DataSource.candidateRolesMap.get(activityExecution
				.getActivity().getId());
		Set<String> candidateUsers = new HashSet<String>();

		for (Role role : roles) {
			Set<User> users = role.getUsers();
			for (User user : users) {
				candidateUsers.add(user.getName());
			}
		}
		return new ArrayList<String>(candidateUsers);
	}

	@Override
	public List<String> findCandidateGroups(Execution execution) {
		ActivityExecution activityExecution = (ActivityExecution) execution;

		Set<Role> roles = DataSource.candidateRolesMap.get(activityExecution
				.getActivity().getId());
		Set<String> candidateGroups = new HashSet<String>();

		for (Role role : roles) {
			candidateGroups.add(role.getName());
		}
		return new ArrayList<String>(candidateGroups);
	}
}
